package com.zengdada.authority.controller;

import com.github.pagehelper.PageInfo;
import com.zengdada.authority.model.TimedTask;
import com.zengdada.authority.service.TimedTaskAuthorityService;
import com.zengdada.authority.utils.JsonMessage;
import com.zengdada.authority.utils.mapper.Page;
import com.zengdada.authority.utils.mapper.Sort;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static com.zengdada.authority.utils.JsonMessage.fail;
import static com.zengdada.authority.utils.JsonMessage.success;

/**
 * @Auther: 12614
 * @Contact: 1261438511@qq.com
 * @Date: 2019-03-29 14:56
 * @Description:
 */
@Controller
@RequestMapping("/authority/timed_task")
public class TimedTaskAuthorityController {

    @Autowired
    @Qualifier("timedTaskAuthorityServiceImpl")
    TimedTaskAuthorityService timedTaskAuthorityService;

    @Value("${authority.application.id}")
    String APPLICATION_ID;

    @GetMapping(value = "/list.html")
    public String listPage(HttpServletRequest request) {
        request.setAttribute("title", "定时任务 管理");
        return "authority/timed_task/list";
    }

    @PostMapping(value = "/list.do")
    @ResponseBody
    public JsonMessage list(@RequestParam(value = "searchs", required = false) String searchs, Page page, Sort sort) {
        try {
            PageInfo<Map<String, Object>> pageInfo = timedTaskAuthorityService.selectDetailedPageInfoByParam(searchs, page, sort);
            return success(pageInfo.getList(), pageInfo.getTotal());
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }

    @GetMapping(value = "/edit.html")
    public String edit(HttpServletRequest request, String id) {
        request.setAttribute("id", id);
        request.setAttribute("applicationId", APPLICATION_ID);
        request.setAttribute("title", StringUtils.isBlank(id) ? "定时任务 新增" : "定时任务 编辑");
        return "authority/timed_task/edit";
    }

    @PostMapping(value = "/save.do")
    @ResponseBody
    public JsonMessage save(TimedTask timedTask) {
        try {
            return success(timedTaskAuthorityService.save(timedTask), null);
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }

    @PostMapping(value = "/detail/{id}.do")
    @ResponseBody
    public JsonMessage detail(@PathVariable(value = "id") String id) {
        try {
            return success(timedTaskAuthorityService.selectDetailedById(id));
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }


    @PostMapping(value = "/del.do")
    @ResponseBody
    public JsonMessage del(@RequestParam(value = "id") String id) {
        try {
            return success(timedTaskAuthorityService.deleteById(id));
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }

    @PostMapping(value = "/change_task_state.do")
    @ResponseBody
    public JsonMessage changeTaskState(@RequestParam(value = "id") String id, @RequestParam(value = "stateCode", defaultValue = "") String state) {
        try {
            return success(timedTaskAuthorityService.changeTaskState(id, state), null);
        } catch (Exception e) {
            return fail("任务状态修改异常：" + e.getMessage());
        }
    }
}
